const api = require('../../../config/api.js');
const app = getApp();
let that;
Page({
  data: {
    ImgRootUrl: app.globalData.ImgRootUrl,
    userInfo: {},
    isLogin: false,
    showLoginDialog: false
  },
  onLoad: function(options) {
    //页面初始化 options为页面跳转所带来的参数
    that = this;
  },
  onReady: function() {},
  onShow: function() {
    //页面显示
    //检查登陆态
    let token = wx.getStorageSync('token');
    if (!token) {
      this.setData({
        userInfo: app.globalData.userInfo,
        isLogin: false
      });
      this.onWechatLogin();   //自动登录
    } else {
      this.setData({
        userInfo: wx.getStorageSync('userInfo'),
        isLogin: true
      });
    }
  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  },
  onUserInfoClick() {
    //点击用户头像
    let token = wx.getStorageSync('token');
    if (!token) {
      this.showLoginDialog();
    }else{
      this.showLoginDialog();
    }
  },
  onDialogBody() {
    // 阻止冒泡
  },
  //显示登录框
  showLoginDialog() {
    this.setData({
      showLoginDialog: true
    })
  },
  //关闭登录框
  onCloseLoginDialog() {
    this.setData({
      showLoginDialog: false
    })
  },
  //Code静默登陆
  onWechatLogin() {
    app.user.Login().then((res) => {
      if (res.errno === 0) {
        //成功
        that.setData({
          userInfo: res.data.userInfo,
          showLoginDialog: false,
          isLogin: true
        });
        app.globalData.Refresh = true;  //刷新首页
      } else {
        that.setData({
          showLoginDialog: false
        });
        //失败，跳转到用户授权页
        wx.navigateTo({
          url: '/pages/auth/login/login'
        })
      }
    }).catch((err) => {
      console.log(err)
    });
  },
  //切换账号
  exitLogin: function() {
    wx.showModal({
      title: '',
      confirmColor: '#b4282d',
      content: '切换账号？',
      success: function(res) {
        if (res.confirm) {
          that.setData({
            userInfo: app.globalData.userInfo,
            isLogin: false
          });
          app.globalData.Refresh = true;
          app.user.Logout().then((res) => {
            wx.navigateTo({
              url: '/pages/auth/login/login'
            })
          });          
        }
      }
    })
  },
  //获取电话号码
  getPhoneNumber(e) {
    if (e.detail.iv === undefined && e.detail.encryptedData === undefined){
      return false;
    }
    app.util.request(api.GetPhoneNumber, { iv: e.detail.iv, encryptedData: e.detail.encryptedData}, 'POST').then(function (res) {
      if (res.errno === 0) {
        console.log(res);
        //刷新首页
        app.globalData.Refresh = true;
        that.setData({
          userInfo: res.data.userInfo,
          showLoginDialog: false
        });
      }else{
        app.util.showToast(res.error);
      }
    });
  },
  callService: function () {
    wx.makePhoneCall({
      phoneNumber: '089865399901'
    })
  },
  clear(e) {
    app.cache.clear();
    wx.showToast({
      title: '清理成功',
      icon: 'success',
      mask: true,
      duration: 2000
    });
    this.setData({
      userInfo: app.globalData.userInfo,
      //showLoginDialog: true,
      isLogin: false
    });
  },
})